﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace JXT.PrimaryKey.Batman.Reflection
{
    /// <summary>
    /// Lambda方法对象
    /// </summary>
    public sealed class LambdaMethodInfo
    {
        /// <summary>
        /// 对应的实际方法
        /// </summary>
        public MethodInfo Method { get; private set; }

        private readonly Lazy<Func<object, object[], object>> invoker;
        /// <summary>
        /// 方法执行器
        /// </summary>
        public Func<object, object[], object> Invoker
        {
            get
            {
                return invoker.Value;
            }
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="method">方法对象</param>
        internal LambdaMethodInfo(MethodInfo method)
        {
            Method = method;
            invoker = new Lazy<Func<object, object[], object>>(() => method.GetLambdaInvoker(), true);
            //Invoker = method.GetLambdaInvoker();
        }

        /// <summary>
        /// 运行方法
        /// </summary>
        /// <param name="instanse">对象</param>
        /// <param name="parameters">参数</param>
        /// <returns>执行结果</returns>
        public object Invoke(object instanse, params object[] parameters)
        {
            return Invoker(instanse, parameters);
        }
    }
}
